/*
 * Copyright (c) 2018-2024 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#ifndef BITSET_H
#define BITSET_H

#include <rpc/types.h>
#include <string.h>

#include "list.h"

#define SET_NUM_ENTS(s) ((s) == NULL ? 0 : (s)->num_element)
#define BYTES_IN_MASK(x) (x)*(sizeof(unsigned int))
#define SET_GET_WORD(position) (position/WORDLENGTH)
#define SET_GET_BIT_IN_WORD(position) (position % WORDLENGTH);

#define WORDLENGTH (sizeof(unsigned int)*8)

#define SET_DEFAULT_SIZE WORDLENGTH
#define SET_WORD_DEFAULT_EXTENT 2

#define SET_UNION      0
#define SET_INTERSECT  1
#define SET_DIFFERENCE 2
#define SET_ASSIGN     5

typedef struct {
    unsigned int *bitmask;
    unsigned int size;
    unsigned int width;
    unsigned int num_element;
    int (*get_index)(void *);
    void *(*get_object)(int);
} BITSET_T;

enum bitState {
    SET_BIT_OFF,
    SET_BIT_ON
};

extern BITSET_T *gf_set_make(const int, int (*get_index)(void *),
                             void *(*get_object)(int));
extern void gf_set_free(BITSET_T *);
extern bool_t gf_set_ismember(BITSET_T *, void *);
extern int gf_set_install(BITSET_T *, void *);
extern int gf_set_remove(BITSET_T *, void *);
extern void gf_set_clear(BITSET_T *);

#endif
